

let currentPage = 1;

$(".search-filter select").each(function(){
	$(this).on("change", function() {
		currentPage = 1
		queryAllInfo()
	});
})
$(".search-filter i").click(function(){
	currentPage = 1
	queryAllInfo()
})

// 全选/取消全选
$("thead input").click(function(){
	$("tbody input").prop("checked",this.checked)
})
$(".video-management").on("click",".action-icon",function(){
	updateVideoState($(this).parent().find("input").val(),$(this).prop('title'),currentPage)
})

// ----------------↑页面----------------↓ajax----------------


queryAllInfo()
function queryAllInfo(pageNum = 1){
	let allVideoNum = $(".stat-value").eq(0)
	let allDanmakuNum = $(".stat-value").eq(1)
	let allNotExamineVideoNum = $(".stat-value").eq(2)
	let allCommentNum = $(".stat-value").eq(3)
	let title = $(".search-filter input").val()
	let state = $(".search-filter select").val()
	let videoTbody = $(".video-table tbody")
	let pagination = $(".pagination")
	$.ajax({
		url:"backVideoQueryAllInfo",
		type:"post",
		data:{
			title:title,
			state:state,
			pageNum:pageNum
		},
		success:function(data){
			videoTbody.empty()
			if(data.code == 200){
				allVideoNum.text(data.data[4]);
				allDanmakuNum.text(data.data[2]);
				allNotExamineVideoNum.text(data.data[3])
				allCommentNum.text(data.data[1])
				
				let videos = data.data[0].data
				for(let i = 0;i<videos.length;i++){
					let video = videos[i]
					let totalTime = countVideoTime(video.duration);
					let playNum = video.viewCount;
	    			let likeCountNum = video.likeCount;
	    			let state = video.status
	    			if(playNum >= 10000){
	    				playNum = playNum/10000+"万"
	    			}
	    			if(likeCountNum >= 10000){
	    				likeCountNum = likeCountNum/10000+"万"
	    			}
	    			if(state == 0){
						state = "待审核"
						addClass = "status-pending"
					}else if(state == 1){
						state = "已发布"
						addClass = "status-published"
					}else{
						state = "已下架"
						addClass = "status-pending"
					}
					let tr = `
						<tr>
		                    <td class="checkbox-cell"><input type="checkbox"></td>
		                    <td>${video.title}</td>
		                    <td class="thumbnail-cell">
		                        <div class="video-thumbnail"
		                            style=" background: url(${video.coverUrl}) no-repeat;background-size: cover;">
		                        </div>
		                    </td>
		                    <td>${totalTime}</td>
		                    <td>${playNum}</td>
		                    <td>${likeCountNum}</td>
		                    <td class="status-cell"><span class="video-status ${addClass}">${state}</span></td>
		                    <td class="actions-cell">
		                        <div class="table-actions">
		                        	<input type="hidden" value="${video.videoId}">
		                            <div class="action-icon edit" title="上架"><i class="fas fa-edit"></i></div>
		                            <div class="action-icon delete" title="下架"><i class="fas fa-trash"></i></div>
		                        </div>
		                    </td>
		                </tr>
					`
					videoTbody.append(tr)
				}
				$(".pagination").css("display","flex")
				makePageBar(data.data[0])
			}else{
				let div = `
					<div style="width: 148px;margin: 60% 407%;">
	                	<img src="img/ZFZJ.svg">
	                	<p style="text-align: center">没有找到哦~</p>
	                </div>
				`
				videoTbody.append(div)
				$(".pagination").css("display","none")
			}
		}
	})
}

function updateVideoState(videoId,state,page){
	$.ajax({
		url:"updateVideoState",
		type:"post",
		data:{
			videoId:videoId,
			state:state
		},
		success:function(data){
			if(data.code == 200){
				promptAnimation(state+"成功")
				queryAllInfo(page)
			}else{
				promptAnimation(state+"失败")
			}
		}
	})
}

function countVideoTime(totalSecond){
	let hous = parseInt(totalSecond/60/60)
	let minute = parseInt((totalSecond - hous*60*60)/60)
	let second = totalSecond - hous*60*60 - minute*60
	if(hous < 10){
		hous = "0"+hous
	}
	if(minute < 10){
		minute = "0"+minute
	}
	if(second < 10){
		second = "0"+second
	}
	let totalTime;
	if(hous == 0){
		totalTime = minute+":"+second
	}else{
		totalTime = hous+":"+minute+":"+second
	}
	return totalTime;
}


function makePageBar(page) {
	$("thead input").prop("checked",false)
    let pageBar = $(".pagination");
    let navigatorNums = page.totalPageNum;
    pageBar.empty();

        let prevPage = $("<button><i class='fas fa-chevron-left'></i></button>").click(function() {
        	queryAllInfo(currentPage - 1);
            currentPage--
        });
        pageBar.append(prevPage);

    for (let i = 0; i < navigatorNums.length; i++) {
        let num = navigatorNums[i];
        let pageLink = $("<button>"+num+"</button>").click(function() {
        	queryAllInfo(num);
            currentPage = num 
        });
        if (num == currentPage) {
            pageLink.addClass("active");
        }
        pageBar.append(pageLink);
    }

        let nextPage = $(" <button><i class='fas fa-chevron-right'></i></button>").click(function() {
        	queryAllInfo(currentPage + 1);
            currentPage++

            
        });
        pageBar.append(nextPage);
        

        if (!page.hasPrev) {
        	prevPage.prop("disabled",true)
        }
        if (!page.hasNext) {
        	nextPage.prop("disabled",true)
        }
        
        
        let start = (page.pageNum - 1) * page.pageSize + 1;
        let end = page.pageNum * page.pageSize;
        end = end > page.totalRows ? page.totalRows : end;
        let bottomPage = $("<div class='pagination-info'>显示"+`${start}`+"-"+`${end}`+" 条，共 "+`${page.totalRows}`+"条</div>");
        pageBar.append(bottomPage)
}

function promptAnimation(text) {
    let $prompt = $("#prompt");
    // 确保元素回到初始位置
    $prompt.css("top", "-60px");
    $prompt.text(text)
    // 上升动画
    $prompt.animate({ top: 0 }, {
        duration: 240,  // 60px / (5px/20ms) = 240ms
        easing: "linear",
        complete: function() {
            // 等待1秒后执行下降动画
            setTimeout(function() {
                // 下降动画
                $prompt.animate({ top: -60 }, {
                    duration: 240,
                    easing: "linear"
                });
            }, 1500);
        }
    });
}
